<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\PackageSetting;
use Illuminate\Http\Request;

class PackageSettingController extends BaseController
{

    public $model = PackageSetting::class;

    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $users = PackageSetting::orderBy('id', 'asc')
                ->paginate(\request('per_page', 20));
        return $this->paginate($users);
    }

    public function store(Request $request) {
        $input = $request->input();
        $id = \request("id");
        $input['month_price'] = $input['month_price_yuan'] * 100;
        $input['admin_id'] = $this->getUserId();
        if ($id) {
            $one = $this->model::find($id);
            if (!$one) {
                return $this->failed('ID不存在');
            }
            if (!$one->can_delete) {
                unset($input['month']);
            }
            $one->update($input);
        } else {
            $info = $this->model::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'ids' => 'required'
        ]);
        $ids = $request->input('ids');
        $arr_id = explode(",", $ids);
        foreach ($arr_id as $id) {
            $model = PackageSetting::find($id);
            if (!$model->can_delete) {
                return $this->failed('无权删除改选项');
            }
            $model->delete();
        }
        return $this->success([]);
    }

}

